package fileutil

import (
	"fmt"
	"nodemessage.com/codetool/util/strutil"
	"os"
)

func MkdirPath(path string) (err error) {
	_, err = os.Stat(path)
	if err != nil {
		if err = os.MkdirAll(path, os.ModePerm); err != nil {
			return
		}
		fmt.Println(strutil.Info + path + " 创建成功")
	}
	return
}

func WriteToFile(path string, content []byte) (err error) {
	fileInstance, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
	if err != nil {
		fmt.Println(strutil.Error + path + " 文件打开失败")
	}
	defer fileInstance.Close()
	_, err = fileInstance.Write(content)
	if err == nil {
		fmt.Println(strutil.Info + path + " 创建成功")
	}
	return
}

func Exist(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return true
}
